package com.pingan.mybatis.decode;

import com.pingan.mybatis.utils.DESUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * @AUTHOR:LIUCHAO;
 * @DATE: 2020/11/27 9:45
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    protected final Logger logger = LoggerFactory.getLogger(EncryptPropertyPlaceholderConfigurer.class);
    // 属性需与配置文件的KEY保持一致
    private String[] encryptPropNames = { "jdbc.user", "jdbc.password", "jdbc.SITuser", "jdbc.SITpassword",
            "jdbc.PRDuser", "jdbc.PRDpassword" };

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        // 如果在加密属性名单中发现该属性
        if (isEncryptProp(propertyName)) {
            logger.info(propertyName + "======>>" + propertyValue);
//            String decryptValue = AESUtil.getDecryptString(propertyValue);
            String decryptValue = DESUtil.getDecryptString(propertyValue);
            System.out.println(decryptValue);
            return decryptValue;
        } else {
            return propertyValue;
        }

    }

    private boolean isEncryptProp(String propertyName) {
        for (String encryptName : encryptPropNames) {
            if (encryptName.equals(propertyName)) {
                return true;
            }
        }
        return false;
    }


}
